package com.example.joke.custom.stepview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.example.joke.R

class QQStepView : View {

    private var mBtmColor = Color.GREEN
    private var mTopColor = Color.RED
    private var mBorderWidth = 20   // 20px
    private var mTextSize = 30
    private var mTextColor = Color.BLACK
    private var mPaintBtm : Paint
    private var mPaintTop : Paint
    private var mPaintText : Paint
    // 最大步数
    var mStepMax = 0
    // 当前步数
    var mCurrentQQStep = 0
        set(value) {
            field = value
            // 对于值得每一次改变，调用重绘，所以形成动画效果
            invalidate()
        }

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typedArray = context?.obtainStyledAttributes(attrs, R.styleable.QQStepView)
        mBtmColor = typedArray?.getColor(R.styleable.QQStepView_btmColor, mBtmColor)!!
        mTopColor = typedArray.getColor(R.styleable.QQStepView_topColor, mTopColor)!!
        mBorderWidth =
            typedArray.getDimension(R.styleable.QQStepView_borderWidth, mBorderWidth.toFloat()).toInt()!!
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, mTextSize)
        mTextColor = typedArray.getColor(R.styleable.QQStepView_stepTextColor, mTextColor)
        typedArray.recycle()

        mPaintBtm = Paint()
        mPaintBtm.isAntiAlias = true
        mPaintBtm.color = mBtmColor
        mPaintBtm.strokeWidth = mBorderWidth.toFloat()
        mPaintBtm.style = Paint.Style.STROKE
        mPaintBtm.strokeCap = Paint.Cap.ROUND

        mPaintTop = Paint()
        mPaintTop.isAntiAlias = true
        mPaintTop.color = mTopColor
        mPaintTop.strokeWidth = mBorderWidth.toFloat()
        mPaintTop.style = Paint.Style.STROKE
        mPaintTop.strokeCap = Paint.Cap.ROUND

        mPaintText = Paint()
        mPaintText.isAntiAlias = true
        mPaintText.color = mTextColor
        mPaintText.textSize = mTextSize.toFloat()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        // 保证是个正方形，取长和宽中最小值作为宽高
        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)

        width = if (width > height) height else width
        height = if (width > height) height else width

        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        // 1. 画底部不动的圆弧，首先计算绘画区域
        // 描边模式/填充与描边模式 实际显示半径 r = 圆半径 + （画笔的宽度 / 2）
        // 所以不可以从原点开始画，也不可以用宽高作为绘画区域，要给多出来的一半宽度预留位置
//        val rectF = RectF(0F, 0F, width.toFloat(), height.toFloat())
        val rectF = RectF(
            (mBorderWidth / 2).toFloat(),
            (mBorderWidth / 2).toFloat(),
            (width - mBorderWidth / 2).toFloat(),
            (height - mBorderWidth / 2).toFloat()
        )
        canvas?.drawArc(rectF, 135f, 270f, false, mPaintBtm)
        // 2. 画上面动的圆弧，首先计算扫过的弧度数
        // 通过当前步数比例，计算出扫过的弧度数，不转float直接相除，得到结果再转float为0.0
        val sweepAngle: Float = mCurrentQQStep.toFloat() / mStepMax * 270
        canvas?.drawArc(rectF, 135f, sweepAngle, false, mPaintTop)
        // 3. 画文字部分，首先计算基线位置和x
        val fontMetrics = mPaintText.fontMetricsInt
        val dy = (fontMetrics.bottom - fontMetrics.top) / 2 -fontMetrics.bottom
        // 3.1 基线的位置为高度的一半加上dy
        val baseLine = height / 2 + dy
        // 3.2 宽度的一半减去文字宽度的一半
        val rect = Rect()
        mPaintText.getTextBounds(mCurrentQQStep.toString(), 0, mCurrentQQStep.toString().length, rect)
        val x = width / 2 - rect.width() / 2
        canvas?.drawText(mCurrentQQStep.toString(), x.toFloat(), baseLine.toFloat(), mPaintText)
    }
}